package com.situ.vivamarket.api.sys;

import com.situ.vivamarket.model.Category;
import com.situ.vivamarket.model.Goods;
import com.situ.vivamarket.service.CategoryService;
import com.situ.vivamarket.service.GoodsService;
import com.situ.vivamarket.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/sys/api/v1/category",produces = MediaType.APPLICATION_JSON_VALUE)
public class CategoryApi {
    private CategoryService categoryService;
    private GoodsService goodsService;
    @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }
    @Autowired
    public void setGoodsService(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    @GetMapping
    public ResponseEntity<JsonResult> list() {

        Category category = this.categoryService.findById(1);
        return ResponseEntity.ok(JsonResult.successData(category.getChildren()));
    }
    @DeleteMapping
    public ResponseEntity<JsonResult> delete(Integer[] ids) {
        if(ids.length==0){
            return ResponseEntity.noContent().build();
        }else{
            for(Integer id:ids){
                List<Goods> goods = goodsService.findByCatId(id,0);
                if(goods.size()!=0){
                    return ResponseEntity.ok(JsonResult.fail("存在有商品的分类，无法进行删除"));
                }
            }
            int count=this.categoryService.deleteByIds(ids);
            try{
                if(count>0){
                    return ResponseEntity.ok(JsonResult.successData(count));
                }else{
                    return ResponseEntity.ok(JsonResult.fail("删除类别失败"));
                }
            }catch (Exception e){
                return ResponseEntity.ok(JsonResult.fail(e.getMessage()));
            }
        }
    }
    @PostMapping
    public ResponseEntity<JsonResult> add(@Validated Category category) {
        if(category==null){
            return ResponseEntity.noContent().build();
        }
        boolean success=this.categoryService.add(category);
        return success?ResponseEntity.ok(JsonResult.successData(category))
                :ResponseEntity.internalServerError().body(JsonResult.fail("新增类别失败"));
    }
    @PutMapping
    public ResponseEntity<JsonResult> update(@Validated Category category) {
        if(category==null){
            return ResponseEntity.noContent().build();
        }else{
            boolean success=this.categoryService.update(category);
            return success?ResponseEntity.ok(JsonResult.successData(category))
                    :ResponseEntity.internalServerError().body(JsonResult.fail("修改类别失败"));
        }
    }

    /**
     * 查询所有商品类别
     *
     * @return 响应json
     */
    @GetMapping("/all")
    public ResponseEntity<JsonResult> findAll() {
        List<Category> categories = this.categoryService.findAll();
        JsonResult result = JsonResult.successData(categories);
        result.setCount(categories.size());
        return ResponseEntity.ok(result);
    }

    /**
     * 查询商品类别树
     *
     * @return 类别树的根节点
     */
    @GetMapping("/tree")
    public ResponseEntity<JsonResult> buildTree() {
        Category root = this.categoryService.buildTree();
        return ResponseEntity.ok(JsonResult.successData(List.of(root)));
    }

}
